package com.ushier.shop.fruits.dao;

import com.ushier.shop.fruits.db.DBHelper;
import com.ushier.shop.fruits.util.BeanUtil;
import com.ushier.shop.fruits.vo.OrderVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderDAO {

    private DBHelper helper = new DBHelper();


    public boolean insert(int userId, String no, Date createTime, String content){
        String sql = "INSERT INTO _order (user_id, no, create_time, content) VALUES(?, ?, ?, ?)";
        int update = helper.update(sql, new Object[]{userId, no, createTime, content});
        return update > 0;
    }


    public List<OrderVO> list(){
        String sql = "SELECT a.id AS id, a.user_id AS userId, a.no AS no, a.create_time AS createTime, a.content AS content, b.name AS userName FROM _order a LEFT JOIN _user b ON a.user_id = b.id";
        List<Map<String, Object>> list = helper.query(sql);
        if(null == list || list.isEmpty()){
            return null;
        }

        List<OrderVO> retList = new ArrayList<>();
        list.forEach(map -> {
            retList.add(BeanUtil.mapToObject(map, OrderVO.class));
        });

        return retList;
    }

}
